package xia.wan.reptile.util;

import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
import xia.wan.reptile.entity.Chapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: reptile
 * @interfaceName HttpClientUtil
 * @description: 调用http请求工具类 获取章节
 * @author: 太白
 * @create: 2022-12-14 18:16
 **/
public class HttpChapter {


    /**
     * 获取章节
     * @return
     */
    public List<Chapter> gainChapter(){
//        此处使用的是漫画连的地址
        String url = "https://www.100mhl.com/manhua/yirenzhixia/";
        ArrayList<Chapter> chapters = new ArrayList<Chapter>();
//        获取整个漫画页面
        String cartoonHtml = HttpUtil.get(url);
//        获取所有的章节标签
        List<String> allLi = ReUtil.findAll("<li class=\"\">(.*?)</li>", cartoonHtml, 1);
//        处理章节标签
        for (String li : allLi) {
            String section = url+this.getSection(li);
            String title = this.getTitle(li);
            Chapter chapter = new Chapter();
            chapter.setName(title);
            chapter.setUrl(section);
            chapters.add(chapter);
        }
        return chapters;
    }

    /**
     * 获取li标签标题
     * @param li
     * @return
     */
    public String getTitle(String li){
        String[] split = li.split(">");
        String aaa = split[1];
        String title = aaa.substring(0, aaa.length() - 2);
        return title;
    }

    /**
     * 处理章节链接
     * @param li
     * @return
     */
    public  String getSection(String li){
        String[] split = li.split("\"");
        String section = split[1];
        return section;
    }
}
